<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 *
 *
 * @package default
 * @author shopexdream.com
 */

 
class ecstoreappadmincoupon_ctl_admin_ecstoreappadmincoupon extends desktop_controller
{

    //礼包位中的商品id 用于生成html、
    private $_filter_goods = null;

    var $workground = 'ecstoreappadmincoupon_ctl_admin_ecstoreappadmincoupon';
 


    function __construct($app)
    {

        parent::__construct($app);
        $this->app=$app;
        //$this->skv();
        $this->_request = kernel::single('base_component_request');
    }

    function index()
    {
        $this->finder('ecstoreappadmincoupon_mdl_ecstoreappadmincoupon', array(
                'title' => app::get('ecstoreappadmincoupon')->_('卡列表'),
                'actions' => array(
                    array('label' => app::get('ecstoreappadmincoupon')->_('生成卡'), 'icon' => 'add.gif', 'href' => 'index.php?app=ecstoreappadmincoupon&ctl=admin_ecstoreappadmincoupon&act=addnew', 'target' => "dialog::{width:400,height:300,title:'生成卡'}",
                    ),


                    /*         array(
                                'label'=>app::get('b2c')->_('批量操作'),
                                'icon'=>'batch.gif',
                                'group'=>$group,
                             ),*/


                    array(
                        'label' => app::get('b2c')->_('导出'),
                        'submit' => 'index.php?app=importexport&ctl=admin_export&act=export_view&_params[app]=ecstoreappadmincoupon&_params[mdl]=ecstoreappadmincoupon_mdl_ecstoreappadmincoupon',
                        'target' => "dialog::{width:400,height:170,title:'卡导出'}",
                    )

                ),
                'use_buildin_set_tag' => true,
                'use_buildin_recycle' => true,
                'use_buildin_filter' => true,
                'use_view_tab' => true,
                'use_buildin_import' => true//导入
            )
        );
    }
    function  send(){

        $this->pagedata['filter']['cpns_id'] = kernel::single('b2c_coupon_filter')->get_coupon();

        $this->page('admin/ecstoreappadmincoupon/new.html');

    }

    public function addnew()
    {

        $this->display('admin/ecstoreappadmincoupon/new.html');
    }

    function dosendbyadmin()
    {
        set_time_limit(0);
        $this->begin();

        $oCoupon = app::get('b2c')->model('coupons');
        $i = 0;
        $saveData = $_POST;
        $oCard = app::get('ecstoreappadmincoupon')->model('ecstoreappadmincoupon');

        if ($_POST['pmt_bond_type'] == '1') {
            foreach ($_POST['generateCoupon'] as $v) {
                foreach ($_POST['member_id'] as $vv) {
                    if ($this->genCouponSk(intval($v), intval($vv), 1)) {
                        $i++;
                    }
                }
            }
        } else {
            $rsm = $oCoupon->db->select("select member_id from sdb_b2c_members where disabled!='true'");
            foreach ($_POST['generateCoupon'] as $v) {
                foreach ($rsm as $kk => $vv) {
                    if ($this->genCouponSk(intval($v), intval($vv['member_id']), 1)) {
                        $i++;
                    }
                }
            }
        }
        if ($i > 0) {
            $this->end(true, app::get('b2c')->_('成功发送' . $i . "张优惠券"));

        } else {
            $this->end(false, app::get('b2c')->_('优惠券发送失败！'));

        }
    }
    /*id优惠券id
    m_id会员id,
    nums-数量*/
        function genCouponSk($id,$m_id,$nums){
            $o = app::get('b2c')->model('coupons');
            $arr = $o->dump($id);
            if( !$arr['rule']['rule_id'] ) return false;
            if($arr['cpns_status'] === '0') return false;
            $arr_rule_order = app::get('b2c')->model('sales_rule_order')->dump( $arr['rule']['rule_id'] );

            if( !$arr_rule_order || $arr_rule_order['to_time']<=time() ) return false;
            if( empty($arr) ) return false;
            $arr_m_c['memc_code'] = $o->_makeCouponCode($arr['cpns_gen_quantity']+1, $arr['cpns_prefix'], $arr['cpns_key']);
            if(!$o->isDownloadAble($arr_m_c['memc_code'])) return false; //是否可下载 B类
            $arr['cpns_gen_quantity'] += 1;
            $o->save($arr);
            $arr_m_c['cpns_id'] = $arr['cpns_id'];
            $arr_m_c['member_id'] = $m_id;
            $arr_m_c['memc_used_times'] = 0;
            $arr_m_c['memc_gen_time'] = time();
           // $params['cpns_point'] = $arr['cpns_point'];
            $params['memc_code'] = $arr_m_c['memc_code'];
            return app::get('b2c')->model('member_coupon')->save($arr_m_c);
        }

}
